﻿Imports System.Data.SqlClient
Public Class AdvTextbox
    Private QueryHeader As String
    Private Key As String
    Private TextboxSource As New TextBox
    Private dsBase As New DataSet
    Public dsResult As New DataSet

    Public Sub Initialize(dsSource As DataSet, KeySource As String, Source As TextBox)
        dsBase = dsSource
        Me.Visible = False
        TextboxSource = Source
        Key = KeySource
        If (dsBase IsNot Nothing) Then
            dgSearch.DataSource = dsBase.Tables(0)
        End If
    End Sub

    Public Sub _Show()
        Me.Visible = True
        Me.Height = 150
        Me.Focus()
    End Sub

    Private Sub AdvTextbox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Height = 20
        'Me.Visible = False
    End Sub

    Private Sub AdvTextbox_Leave(sender As Object, e As EventArgs) Handles MyBase.Leave
        Me.Height = 20
        Me.Visible = False
    End Sub

    Private Sub GetResult(rInd As Integer)
        If (TextboxSource IsNot Nothing) Then
            TextboxSource.Focus()
            Dim rIn As Integer = dgSearch.CurrentRow.Index
            Dim dt As New DataTable
            Dim dr As DataRow
            Dim column As DataColumn
            For i = 0 To dsBase.Tables(0).Columns.Count - 1
                column = New DataColumn(dsBase.Tables(0).Columns(i).ColumnName, dsBase.Tables(0).Columns(i).DataType)
                dt.Columns.Add(column)
            Next
            dr = dt.NewRow()
            For i = 0 To dsBase.Tables(0).Columns.Count - 1
                Dim cName As String = dsBase.Tables(0).Columns(i).ColumnName
                dr(cName) = dgSearch.Rows(rInd).Cells(cName).Value.ToString()
            Next
            dt.Rows.Add(dr)
            dsResult.Tables.Clear()
            dsResult.Tables.Add(dt)
            txtBox.Text = ""
            TextboxSource.Text = "load"
            TextboxSource.Focus()
        End If
    End Sub

    Private Sub dgSearch_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgSearch.CellDoubleClick
        GetResult(e.RowIndex)
    End Sub

    Private Sub txtBox_TextChanged(sender As Object, e As EventArgs) Handles txtBox.TextChanged
        If (dsBase IsNot Nothing) Then
            Dim searchvalue As String = txtBox.Text.Trim()
            Dim dvFind As DataView = New DataView(dsBase.Tables(0))
            Dim wQuery As String = ""
            Dim max As Integer = dsBase.Tables(0).Columns.Count - 1
            For i = 0 To max
                If (dsBase.Tables(0).Columns(i).DataType.ToString() <> "System.DateTime") Then
                    Dim Column As String = dsBase.Tables(0).Columns(i).ColumnName.ToString()
                    wQuery += "CONVERT([" & Column & "] ,System.String) LIKE '%" & searchvalue & "%'"
                    If (i < max) Then
                        wQuery += " OR "
                    End If
                End If
            Next

            dvFind.RowFilter = wQuery
            dgSearch.DataSource = dvFind
        End If
    End Sub

    Private Sub AdvTextbox_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown, txtBox.KeyDown, dgSearch.KeyDown
        If (e.KeyCode = Keys.Escape) Then
            txtBox.Text = ""
            Me.Visible = False
            TextboxSource.Focus()
        End If
        If (dgSearch.Rows.Count > 0) Then
            If (e.KeyCode = Keys.Enter) Then
                GetResult(dgSearch.CurrentRow.Index)
            Else
                Dim sInd As Integer = dgSearch.CurrentRow.Index
                If (e.KeyCode = Keys.Down) Then
                    If (sInd < dgSearch.Rows.Count - 1) Then
                        sInd += 1
                    End If
                End If
                If (e.KeyCode = Keys.Up) Then
                    If (sInd > 0) Then
                        sInd -= 1
                    End If
                End If
                dgSearch.ClearSelection()
                dgSearch.CurrentCell = dgSearch.Rows(sInd).Cells(0)
                dgSearch.Rows(sInd).Selected = True
            End If
        End If
    End Sub
End Class
